package com.byzk.invoice.xml.Doc_0.Attachs;

import com.byzk.invoice.util.DocumentUtil;
import org.dom4j.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建Attachments.xml.
 *
 * @author 康鹏 2020-05-20
 */
public class Attachments {

    /**
     * 创建Doc_0/Attachs/Attachments.xml.
     * @param attachmentsXmlPath Attachments.xml存放路径
     * @throws Exception Exception
     */
    public static void createAttachmentsXml(String attachmentsXmlPath) throws Exception {

        Namespace namespace = new Namespace("ofd", "http://www.ofdspec.org/2016");
        //创建外层<ofd:Attachments />
        Element rootElt = DocumentHelper.createElement(new QName("Attachments", namespace));
        //创建<ofd:Attachment />
        Element attachmentElt = rootElt.addElement("ofd:Attachment");
        attachmentElt.addAttribute("ID", "139");
        attachmentElt.addAttribute("Name", "original_invoice");
        attachmentElt.addAttribute("Format", "xml");
        attachmentElt.addAttribute("CreationDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        attachmentElt.addAttribute("Size", "2.9248");
        attachmentElt.addAttribute("visible", "false");
        //创建<ofd:FileLoc />
        Element fileLocElt = attachmentElt.addElement("ofd:FileLoc");
        fileLocElt.setText("original_invoice.xml");

        Document document = DocumentHelper.createDocument(rootElt);
        DocumentUtil.writer(document, attachmentsXmlPath + File.separator + "Doc_0" + File.separator + "Attachs"
                + File.separator + "Attachments.xml", true);
    }

}
